package top.hkyzf.study.leetcode.string;

import org.junit.Test;

/**
 * description: 
 * @author zhufeng
 * @date 2024/4/10 0010 10:40
 * @version V1.0
 */
public class 修改后的最大二进制字符串_1702 {
    public String maximumBinaryString(String binary) {
        int poCount = 0;
        int zCount = 0;
        int oCount = 0;
        for (int i = 0; i < binary.length(); i++) {
            if (binary.charAt(i) == '1') {
                poCount++;
            } else {
                break;
            }
        }
        for (int i = poCount; i < binary.length(); i++) {
            if (binary.charAt(i) == '0') {
                zCount++;
            } else {
                oCount++;
            }
        }
        if (zCount == 1) {
            return binary;
        } else {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < poCount; i++) {
                sb.append('1');
            }
            for (int i = 0; i < zCount; i++) {
                if (i == zCount-1) {
                    sb.append('0');
                } else {
                    sb.append('1');
                }
            }
            for (int i = 0; i < oCount; i++) {
                sb.append('1');
            }
            return sb.toString();
        }
    }

    @Test
    public void testMaximumBinaryString () {
        String binary = "000110";
        System.out.println(maximumBinaryString(binary));
    }
}
